// pages/operation/orderManagement/orderIndex/index.js
import { Order } from "../../../../api/api-work/order.js";
const order = new Order();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 15,
    param: "",
    sumAmount: "0",
    tableDatas: [],
    showOperation: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      istriggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getSumAmount();
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },

  getSumAmount() {
    order
      .getSumAmountAPI({
        studentName: this.data.param,
      })
      .then((res) => {
        this.setData({
          sumAmount: res,
        });
      });
  },

  getOrderList() {
    order
      .getOrderListAPI({
        studentName: this.data.param,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      })
      .then((res) => {
        let rows = this.data.tableDatas;
        rows.push(...res.rows);
        this.setData({
          tableDatas: rows,
          total: res.total,
        });

        if (res.total == 0) {
          this.setData({
            // 无数据
            loadStatus: "",
          });
        } else if (rows.length < res.total) {
          // 还有数据
          this.setData({
            // 加载更多
            loadStatus: "-",
          });
        } else {
          // 没有数据了
          this.setData({
            // 加载更多
            loadStatus: "nomore",
          });
        }
      });
  },

  searchStudent() {
    this.setData({
      pageNum: 1,
      tableDatas: [],
    });
    this.getSumAmount();
    this.getOrderList();
  },

  refreshMoreDatas() {
    this.setData({
      istriggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getOrderList();
  },
  getMoreDatas() {
    if (this.data.loadStatus === "nomore") {
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading", // 加载中
    });
    this.getOrderList();
  },
  toOperation() {
    this.setData({
      showOperation: true,
    });
  },
  closeOperation() {
    this.setData({
      showOperation: false,
    });
  },

  toDetails(e) {
    const param = JSON.stringify({
      id: e.currentTarget.dataset.query.id
    })
    wx.navigateTo({
      url: `/pages/operation/student/orderDetails/index?params=${param}`,
    })
  },
  toPage(e) {
    this.closeOperation();
    wx.navigateTo({
      url: `/pages/operation/signUp/signUpIndex/index?operateType=${e.currentTarget.dataset.query}`,
    });
  },
});
